<?php


namespace app\api\controller;

use app\common\model\BusOrderModel;
use app\common\model\GoodslistModel;
use app\common\model\OrderitemMoel;
use think\Request;
use app\common\model\AdminModel;

class User extends Base
{
    public function listuser(){

    }
    public function insertuser(){ //增

    }
    public function updateuser(Request $request)
    {//改
        $admin = new AdminModel();
        $userid = $request->param('id');
        $info = $request->param();
        if($info['role']==3)
        {
            $password = $admin->where('id', $info['id'])->where('password', $info['oldpassword'])->find();
            if (!$password) {
                unset($info['password']);
            }
            unset($info['oldpassword']);
            if ($info['sex'] != '男' && $info['sex'] != '女') {
                $info['sex'] = '男';
            }
            if ($info['email'] != "" && !filter_var($info['email'], FILTER_VALIDATE_EMAIL))
                return json(['code' => 0, 'msg' => '非法邮箱字符']);
            if ($info['email'] == "")
                unset($info['email']);
            if ($info['age'] != "" && !preg_match('~^[0-9]+$~', $info['age'])) return json(['code' => 0, 'msg' => '非法年龄输入']);
            else {
                if ($info['age'] == "")
                    unset($info['age']);
                else {
                    $age = number_format($info['age']);
                    if ($age > 120 || $age < 12) return json(['code' => 0, 'msg' => '年龄不合理']);
                }
            }
            if ($info['qq'] != "" && !preg_match('~^[0-9]+$~', $info['qq'])) return json(['code' => 0, 'msg' => '非法qq字符']);
            else {
                if ($info['qq'] == "")
                    unset($info['qq']);
                else {
                    $len = strlen($info['qq']);
                    if ($len < 6 || $len > 10) return json(['code' => 0, 'msg' => 'qq长度不规范']);
                }
            }
            if(!isset($info['telephone'])||$info['telephone']=="")
                return json(['code' => 0, 'msg' => '未填写联系方式']);
            if ($info['telephone'] != "" && !preg_match('~^[0-9]+$~', $info['telephone'])) return json(['code' => 0, 'msg' => '非法联系方式']);
            else {
                if ($info['telephone'] == "")
                    unset($info['telephone']);
                else {
                    $len = strlen($info['telephone']);
                    if ($len != 11) return json(['code' => 0, 'msg' => '联系方式长度不规范']);
                }
            }
            if($info['addr']=="")
                return json(['code' => 0, 'msg' => '未填写地址']);
            if($info['addr']=="未设置地址")
                unset($info['addr']);
            if ($info) {
                $admin->save($info, ['id' => $userid]);
                return json(['code' => 1, 'msg' => '修改成功', 'data' => $info]);
            } else return json(['code' => 0, 'msg' => '用户id不存在']);
        }
        if($info['role']==2)
        {
            $password = $admin->where('id', $info['id'])->where('password', $info['oldpassword'])->find();
            if (!$password) {
                unset($info['password']);
            }
            unset($info['oldpassword']);
            if(!isset($info['telephone'])||$info['telephone']=="")
                return json(['code' => 0, 'msg' => '未填写联系方式']);
            if ($info['telephone'] != "" && !preg_match('~^[0-9]+$~', $info['telephone']))
                return json(['code' => 0, 'msg' => '非法联系方式']);
            else {
                if ($info['telephone'] == "")
                    unset($info['telephone']);
                else {
                    $len = strlen($info['telephone']);
                    if ($len != 11) return json(['code' => 0, 'msg' => '联系方式长度不规范']);
                }
            }
            if(!isset($info['addr'])||$info['addr']=="")
                return json(['code' => 0, 'msg' => '未填写地址']);
            if($info['addr']=="未设置地址")
                unset($info['addr']);
            if ($info) {
                $admin->save($info, ['id' => $userid]);
                return json(['code' => 1, 'msg' => '修改成功', 'data' => $info]);
            } else return json(['code' => 0, 'msg' => '用户id不存在']);
        }
    }
    public function info(Request $request){ //详情
        $userid=$request->param("userid");
        $admin=new AdminModel();
        $info=$admin->where("status",0)->where("id",$userid)->find();
        unset($info['status']);
        unset($info['role']);
        unset($info['is_recommended']);
        unset($info['create_time']);
        unset($info['update_time']);
        unset($info['password']);

        if(!empty($info))
            return json(['code'=>1,'msg'=>'获取用户信息成功','data'=>$info]);
        else
            return json(['code'=>0,'msg'=>'获取用户信息失败']);

    }
    public function deleteuser(){//删除

    }
    public function changestate(Request $request){  ///商家修改订单状态|销量增加
        $id=$request->param('orderid');
        $busid=$request->param('busid');
        $state=$request->param('state');

        $busorder=new BusOrderModel();
        $orderitem=new OrderitemMoel();
        $bus=new AdminModel();
        $goodslist=new GoodslistModel();

        $busorder->where('orderid',$id)->where('busid',$busid)->update(['state'=>$state]);
        $bsorder=$busorder->where('orderid',$id)->where('busid',$busid)->find();
        if($state==2)
        {
            //商家完成订单数增加
            $bus->where('id',$busid)->inc('order_finished_number',1)->update();
            //销量增加操作
            $res=$orderitem->where('busorderid',$bsorder['id'])->select();
            foreach($res as $key => $value)
            {
                $temp=$value['count'];
                $goodslist->where('id',$value['goods_id'])->inc('sellcount',$temp)->update();
            }
        }
        else if($state==3)
            return json(['code' => 0, 'msg' => '订单已取消']);

        return json(['code' => 1, 'msg' => '接单成功']);
    }

    public function score(Request $request)
    {
        $id=$request->param('id');
        $score=$request->param('score');
        $orderid=$request->param('orderid');
        $goodslist=new GoodslistModel();
        $orderitem=new OrderitemMoel();

        $orderitem->where('orderid',$orderid)->where('goods_id',$id)->update(['score_state'=>1,'score'=>$score]);
        $goodslist->where('id',$id)->inc('total_score',$score)->inc('people_score_number',1)->update();

        return json(['code'=>1,'msg'=>'打分成功']);
    }

}